package com.smedi.ismedi.dangeroussourcemanage.core.infrastructure.util;

import com.smedi.ismedi.dangeroussourcemanage.core.application.command.FileCommand;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;

import java.io.InputStream;

@Slf4j
public abstract class ExportFiles {
    public FileCommand creatExcel(String path) {
        try {
            ClassPathResource classPathResource = new ClassPathResource(path);
            InputStream inputStream = classPathResource.getInputStream();
            XSSFWorkbook workBook = null;
            try {
                workBook = new XSSFWorkbook(inputStream); // 使用XSSFWorkbook
            } finally {
                if (inputStream == null) {
                    inputStream.close();
                }
            }
            //设置单元格有上下左右边框
            BorderStyle borderStyle = BorderStyle.THIN;
            CellStyle style = workBook.createCellStyle();
            style.setBorderTop(borderStyle);
            style.setBorderBottom(borderStyle);
            style.setBorderLeft(borderStyle);
            style.setBorderRight(borderStyle);
            //拷贝模板所在的sheet，第一个参数的sheet的下标，从0开始；第二个参数为buildingSheet的名称
            XSSFSheet buildingSheet = workBook.cloneSheet(0, "xxx");
            XSSFCellStyle cellStyle = workBook.createCellStyle();
            //启动单元格内换行
            cellStyle.setWrapText(true);
            return new FileCommand(buildingSheet, workBook);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }
}
